<?php

class URLParser
{
    /**
     * Parses the given URL segment, according to this format:
     * "module/action/param1:value1/param2:value2"
     * @param string $str The string to be parsed
     * @return array 
     */
    public static function parse($str)
    {
        $parts=explode(URL_DELIMITER, $str);
        $parsed=array();
        $parsed["module"]=isset($parts[0])?$parts[0]:DEFAULT_MODULE;
        $parsed["action"]=isset($parts[1])?$parts[1]:DEFAULT_ACTION;
        $parsed["params"]=array();
        if(count($parts)>2)
        {
            for($i=2;isset($parts[$i]);$i++)
            {
                $sub=explode(URL_SEPARATOR, $parts[$i]);
                if(count($sub)!=2)
                    throw new RequestException(sprintf("Bad Request Format! (%s)", $str));
                $parsed["params"][$sub[0]]=$sub[1];
            }
        }
        return $parsed;
    }
}
